import 'dart:math';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';

import 'NoteItem.dart';
import 'NoteListItemWidget.dart';

class DisplayPage extends StatefulWidget {
  const DisplayPage({super.key});

  @override
  State<DisplayPage> createState() => _DisplayPageState();
}

class _DisplayPageState extends State<DisplayPage> {
  int _count = 10;
  late EasyRefreshController _controller;

  bool isLoading = false;
  ScrollController scrollController = ScrollController();
  List<NoteItem> list = [];

  List<NoteImage> noteImages = [];

  String randomBit() {
    String scopeF = '0123456789'; //首位
    String result = '';
    result = scopeF[Random().nextInt(scopeF.length)];
    return result;
  }

  @override
  void initState() {
    initItems();
    super.initState();
    _controller = EasyRefreshController(
      controlFinishRefresh: true,
      controlFinishLoad: true,
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  void initItems() {
    for (int index = 0; index < 12; index++) {
      NoteImage model = NoteImage();
      noteImages.add(model);

      if (index == 0) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/blog/202010/11/20201011085349_R2CjU.thumb.1000_0.jpeg";
        model.imageWidth = "580";
        model.imageHeight = "880";
      } else if (index == 1) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/blog/202011/15/20201115080806_7ddba.thumb.1000_0.jpg";
        model.imageWidth = "580";
        model.imageHeight = "580";
      } else if (index == 2) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/blog/202011/15/20201115100436_7ffc6.thumb.1000_0.jpg";
        model.imageWidth = "580";
        model.imageHeight = "560";
      } else if (index == 3) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/item/202006/09/20200609232715_yvqkd.thumb.1000_0.jpg";
        model.imageWidth = "580";
        model.imageHeight = "380";
      } else if (index == 4) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/blog/202012/03/20201203144923_425bc.thumb.1000_0.jpg";
        model.imageWidth = "580";
        model.imageHeight = "430";
      } else if (index == 5) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/blog/202010/11/20201011085351_3d2cS.thumb.1000_0.jpeg";
        model.imageWidth = "580";
        model.imageHeight = "850";
      } else if (index == 6) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/blog/202009/28/20200928184639_sxrum.thumb.1000_0.jpeg";
        model.imageWidth = "580";
        model.imageHeight = "810";
      } else if (index == 7) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/blog/202012/03/20201203145208_55d8c.thumb.1000_0.jpg";
        model.imageWidth = "580";
        model.imageHeight = "430";
      } else if (index == 8) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/item/202006/09/20200609232715_pjsin.thumb.1000_0.jpg";
        model.imageWidth = "580";
        model.imageHeight = "860";
      } else if (index == 9) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/blog/202012/03/20201203144929_cd406.thumb.1000_0.jpg";
        model.imageWidth = "580";
        model.imageHeight = "450";
      } else if (index == 10) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/blog/202012/03/20201203144705_40405.thumb.1000_0.jpg";
        model.imageWidth = "580";
        model.imageHeight = "450";
      } else if (index == 11) {
        model.imageUrl =
        "https://c-ssl.duitang.com/uploads/blog/202012/03/20201203144708_d5287.thumb.1000_0.jpg";
        model.imageWidth = "580";
        model.imageHeight = "750";
      }
    }

    for (int index = 0; index < 20; index++) {
      NoteItem model = NoteItem();
      model.liked = false;
      list.add(model);

      if (index % 5 == 0) {
        model.username = "可可";
        model.avatarUrl =
        "https://c-ssl.duitang.com/uploads/item/202007/22/20200722212206_ifscm.thumb.1000_0.jpg";
        model.time = "08/01 13:51";
        model.textContent = "等轮到你讲话时再说,认真聆听对方的谈话,千万别打断对方讲话,耐心等着,轮到你时再讲。";
        model.likeNum = "861380";
        model.categoryName = "兽圈";
      } else if (index % 5 == 2) {
        model.username = "琪琪";
        model.avatarUrl =
        "https://c-ssl.duitang.com/uploads/item/202007/22/20200722212209_insvm.thumb.1000_0.png";
        model.time = "08/01 13:51";
        model.textContent = "记得有活动，在活动现场等你哦～";
        model.likeNum = "50999";
        model.categoryName = "娃圈";
      } else if (index % 5 == 4) {
        model.username = "悦悦";
        model.avatarUrl =
        "https://c-ssl.duitang.com/uploads/item/202007/19/20200719102754_jrcoe.thumb.1000_0.jpg";
        model.time = "08/01 13:51";
        model.textContent = "说一说你想要的形象模型，晒一晒你的喜欢";
        model.likeNum = "6150";
        model.categoryName = "Pia戏圈";
      } else {
        model.username = "爱丽多啦";
        model.avatarUrl =
        "https://c-ssl.duitang.com/uploads/item/202007/19/20200719102756_ihfku.thumb.1000_0.jpg";
        model.time = "08/01 13:51";
        model.textContent = "你永远不懂我喜欢的东西，555";
        model.likeNum = "1507";
        model.categoryName = "语c圈";
      }

      List<NoteImage> tmpImages = [];
      String randomf = randomBit();
      String randomt = randomBit();
      int f = int.parse(randomf);
      int t = int.parse(randomt);
      print("f:$f");
      print("t:$t");

      if (f < t) {
        tmpImages.addAll(noteImages.sublist(f, t));
      } else if (f > t) {
        tmpImages.addAll(noteImages.sublist(t, f));
      } else {
        tmpImages.addAll(noteImages.sublist(t, t+1));
      }
      model.images = tmpImages;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('列表测试'),
      ),
      body: EasyRefresh(
        controller: _controller,
        header: const ClassicHeader(),
        footer: const ClassicFooter(),
        onRefresh: () async {
          await Future.delayed(const Duration(seconds: 4));
          if (!mounted) {
            return;
          }
          list = [];
          initItems();
          setState(() {
            _count = 10;
          });
          _controller.finishRefresh();
          _controller.resetFooter();
        },
        onLoad: () async {
          await Future.delayed(const Duration(seconds: 4));
          if (!mounted) {
            return;
          }
          initItems();
          setState(() {
            _count += 5;
          });
          _controller.finishLoad(
              _count >= 200 ? IndicatorResult.noMore : IndicatorResult.success);
        },
        child: ListView.builder(
          itemBuilder: (context, index) {
            return NoteListItemWidget(noteItem: this.list[index]);
          },
          itemCount: this.list.length,
        ),
      ),
    );
  }
}

